#!/bin/bash

# ===============================================
# Raspberry Pi Toolchains(raspberry-pi-cross-compilers): This project
# provides latest automated GCC Cross Compiler & Native (ARM & ARM64)
# build-scripts and precompiled standalone toolchains for Raspberry Pi.

# Copyright (C) 2020 Abhishek Thakur(@abhiTronix) <abhi.una12@gmail.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
# ===============================================

# This script auto-deploys CI compiled Cross & Native GCC Toolchain binaries to Sourceforge secured server

#Deploy Cross-compiler Toolchain binaries
if [ -f "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" ]; then
	if [ "$RPI_TYPE" = "64" ]; then
		if [ "$BASE" = "true" ]; then
			if [ "$RPIOS_TYPE" = "buster" ]; then
				FOLDER="CLI Base/64/Buster/GCC $GCC_VERSION"
			else
				FOLDER="CLI Base/64/Stretch/GCC $GCC_VERSION"
			fi
		else
			if [ "$RPIOS_TYPE" = "buster" ]; then
				FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Cross-Compiler Toolchains/Buster/GCC $GCC_VERSION"
			else
				FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Cross-Compiler Toolchains/Stretch/GCC $GCC_VERSION"
			fi
		fi
		if [ "$FOLDER" != "" ]; then
			curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
		fi
	else
		if [ "$RPIOS_TYPE" = "buster" ]; then
			if [ "$BASE" = "true" ]; then
				FOLDER="CLI Base/Buster/GCC $GCC_VERSION"
			else
				FOLDER="Raspberry Pi GCC Cross-Compiler Toolchains/Buster/GCC $GCC_VERSION"
			fi
			if [ "$RPI_TYPE" = "0-1" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 1, Zero/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "2-3" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 2, 3/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "3+" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 3A+, 3B+, 4/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
		fi
		if [ "$RPIOS_TYPE" = "stretch" ]; then
			if [ "$BASE" = "true" ]; then
				FOLDER="CLI Base/Stretch/GCC $GCC_VERSION"
			else
				FOLDER="Raspberry Pi GCC Cross-Compiler Toolchains/Stretch/GCC $GCC_VERSION"
			fi
			if [ "$RPI_TYPE" = "0-1" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 1, Zero/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "2-3" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 2, 3/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "3+" ] && [ "$FOLDER" != "" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 3A+, 3B+, 4/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
		fi
	fi
else
	echo "Cannot find $HOME/cross-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz"
fi

#Deploy Native-compiler Toolchain binaries
if [ -f "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" ]; then
	if [ "$RPI_TYPE" = "64" ]; then
		if [ "$RPIOS_TYPE" = "buster" ]; then
			FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Native-Compiler Toolchains/Buster/GCC $GCC_VERSION"
		else
			FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Native-Compiler Toolchains/Stretch/GCC $GCC_VERSION"
		fi
		curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
	else
		if [ "$RPIOS_TYPE" = "buster" ]; then
			FOLDER="Raspberry Pi GCC Native-Compiler Toolchains/Buster/GCC $GCC_VERSION"
			if [ "$RPI_TYPE" = "0-1" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 1, Zero/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "2-3" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 2, 3/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "3+" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 3A+, 3B+, 4/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
		fi
		if [ "$RPIOS_TYPE" = "stretch" ]; then
			FOLDER="Raspberry Pi GCC Native-Compiler Toolchains/Stretch/GCC $GCC_VERSION"
			if [ "$RPI_TYPE" = "0-1" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 1, Zero/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "2-3" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 2, 3/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
			if [ "$RPI_TYPE" = "3+" ]; then
				curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/Raspberry Pi 3A+, 3B+, 4/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz" --ftp-create-dirs
			fi
		fi
	fi
else
	echo "Cannot find $HOME/native-gcc-$GCC_VERSION-pi_$RPI_TYPE.tar.gz"
fi

if [ -f "$HOME/desktop-gcc-$GCC_VERSION-pi_$OS_TYPE.tar.gz" ]; then
	FOLDER="Exclusive-Experimental Toolchains/Desktop/$OS_TYPE/GCC $GCC_VERSION"
	if [ "$FOLDER" != "" ]; then
		curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/desktop-gcc-$GCC_VERSION-pi_$OS_TYPE.tar.gz" --ftp-create-dirs
	else
		exit 1
	fi
fi
